package band.wukong.practice.springboot.spring4.mvc;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 其实这也是个rest 因为有@Responsebody
 *
 * @author wukong(wukonggg@139.com)
 */

@Controller
@RequestMapping("/hello")
public class HelloController {

    @RequestMapping(produces = "text/plain;charset=UTF-8")
    public @ResponseBody String hello(HttpServletRequest req) {
        // @RequestMapping的value没有写就意味着没有mapping
        return "URI [" + req.getRequestURI() + "] can access.";
    }

    @RequestMapping(value = "/pathVar/{s}", produces = "text/plain;charset=UTF-8")
    public @ResponseBody String pathVar(@PathVariable String s) {
        return s;
    }

    @RequestMapping(value = "/requestParam", produces = "text/plain;charset=UTF-8")
    public @ResponseBody String requestParam(Long id) {
        return String.valueOf(id);
    }

    @RequestMapping(value = "/passObj", produces = "application/json;charset=UTF-8")
    public @ResponseBody String passObj(Hello hello) {
        return hello.toString();
    }

    @RequestMapping(value = {"/multiMapping1", "/multiMapping2"}, produces = "text/plain;charset=UTF-8")
    public @ResponseBody String multiMapping(HttpServletRequest req) {
        return "URI [" + req.getRequestURI() + "] can access.";
    }

}
